<template>
  <div>
    <h2>用户活跃度</h2>

    <div class="toolbar">
      <el-button type="primary" @click="toggleSummaryMode">
        {{ showSummary ? '显示每日数据' : '显示用户汇总数据' }}
      </el-button>
      <el-button type="success" @click="goToBehaviorStats">
        查看行为分类统计
      </el-button>
    </div>

    <el-table v-if="!showSummary" :data="userBehaviorDaily" style="width: 100%">
      <el-table-column prop="userId" label="用户ID" width="180"></el-table-column>
      <el-table-column prop="username" label="用户名称" width="180"></el-table-column>
      <el-table-column prop="viewCount" label="浏览次数"></el-table-column>
      <el-table-column prop="clickCount" label="点击内容次数"></el-table-column>
      <el-table-column prop="likeCount" label="点赞次数"></el-table-column>
      <el-table-column prop="commentCount" label="评论次数"></el-table-column>
      <el-table-column prop="shareCount" label="分享次数"></el-table-column>
      <el-table-column prop="favoriteCount" label="收藏次数"></el-table-column>
      <el-table-column prop="durationSeconds" label="停留时长(秒)">
        <template #default="{ row }">
          {{ row.durationSeconds || '0' }}
        </template>
      </el-table-column>
      <el-table-column prop="statDate" label="日期" width="180">
        <template #default="{ row }">
          {{ formatDate(row.statDate) }}
        </template>
      </el-table-column>
    </el-table>

    <el-table v-else :data="pagedSummaryData" style="width: 100%">
      <el-table-column prop="userId" label="用户ID" width="180"></el-table-column>
      <el-table-column prop="username" label="用户名称" width="180"></el-table-column>
      <el-table-column prop="totalViewCount" label="总浏览次数"></el-table-column>
      <el-table-column prop="totalClickCount" label="总点击次数"></el-table-column>
      <el-table-column prop="totalLikeCount" label="总点赞次数"></el-table-column>
      <el-table-column prop="totalShareCount" label="总分享次数"></el-table-column>
      <el-table-column prop="totalFavoriteCount" label="总收藏次数"></el-table-column>
      <el-table-column prop="totalDurationSeconds" label="总停留时长(秒)">
        <template #default="{ row }">
          {{ row.totalDurationSeconds || '0' }}
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @current-change="handlePageChange" @size-change="handleSizeChange" :current-page="current"
      :page-size="size" :total="showSummary ? summaryTotalItems : totalItems" layout="total, prev, pager, next, sizes"
      :page-sizes="[6, 10, 20, 50]">
    </el-pagination>
  </div>
</template>

<script>
import axios from '@/utils/request';

export default {
  name: 'UserLiveness',
  data() {
    return {
      userBehaviorDaily: [],
      userBehaviorSummary: [],
      pagedSummaryData: [],
      current: 1,
      size: 6,
      totalItems: 0,
      summaryTotalItems: 0,
      showSummary: false
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      if (this.showSummary) {
        this.fetchSummaryData();
      } else {
        this.fetchDailyData();
      }
    },

    fetchDailyData() {
      const url = '/userRetentionStats/daily/page';
      const params = { current: this.current, size: this.size };
      axios.get(url, { params }).then(response => {
        if (response.data && response.data.code === 200) {
          this.userBehaviorDaily = response.data.data.records;
          this.totalItems = response.data.data.total;
        }
      }).catch(error => {
        this.$message.error('加载每日数据失败');
      });
    },

    fetchSummaryData() {
      const url = '/userRetentionStats/daily/summary';
      axios.get(url).then(response => {
        if (response.data && response.data.code === 200) {
          this.userBehaviorSummary = response.data.data;
          this.summaryTotalItems = this.userBehaviorSummary.length;
          this.updatePagedSummaryData();
        }
      }).catch(error => {
        this.$message.error('加载用户汇总数据失败');
      });
    },

    updatePagedSummaryData() {
      const start = (this.current - 1) * this.size;
      const end = start + this.size;
      this.pagedSummaryData = this.userBehaviorSummary.slice(start, end);
    },

    toggleSummaryMode() {
      this.showSummary = !this.showSummary;
      this.current = 1;
      this.fetchData();
    },

    goToBehaviorStats() {
      this.$router.push({ name: 'UserBehaviorCategoryStats' });
    },

    handlePageChange(page) {
      this.current = page;
      this.fetchData();
    },

    handleSizeChange(size) {
      this.size = size;
      this.current = 1;
      this.fetchData();
    },

    formatDate(date) {
      if (!date) return '';
      try {
        const dateObj = typeof date === 'string' ? new Date(date) : date;
        const year = dateObj.getFullYear();
        const month = String(dateObj.getMonth() + 1).padStart(2, '0');
        const day = String(dateObj.getDate()).padStart(2, '0');
        return `${year}-${month}-${day}`;
      } catch (e) {
        return date;
      }
    }
  }
};
</script>

<style scoped>
.toolbar {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.el-table {
  margin-top: 20px;
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}
</style>